package com.reins.bookstore.daoimpl;

import com.reins.bookstore.dao.UserDao;
import com.reins.bookstore.entity.User;
import com.reins.bookstore.entity.UserAuth;
import com.reins.bookstore.object.UserDTO;
import com.reins.bookstore.repository.UserAuthRepository;
import com.reins.bookstore.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName UserDaoImpl
 * @Description the implement of user dao
 * @Author thunderBoy
 * @Date 2019/11/7 13:19
 */
@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    UserAuthRepository userAuthRepository;

    @Autowired
    UserRepository userRepository;

    @Override
    public UserAuth checkUser(String username, String password){
        return userAuthRepository.checkUser(username,password);
    }

    @Override
    public User findUserById(Integer id){
        return userRepository.findById(id).get();
    }

    @Override
    public UserAuth findUserAuthById(Integer id){
        return userAuthRepository.findById(id).get();
    }

    @Override
    public UserAuth findUserAuthByName(String username){
        return userAuthRepository.findByName(username);
    }

    @Override
    public List<User> findAllUsers(){
        return userRepository.findAll();
    }

    @Override
    public List<UserAuth> findAllUserAuths(){
        return userAuthRepository.findAll();
    }


    @Override
    public List<UserDTO> findAllUserDTOs(){
        List<User> all_user = findAllUsers();
        List<UserAuth> all_user_auth = findAllUserAuths();
        List<UserDTO> res = new LinkedList<>();
        int len = all_user.size();
        User get_user = null;
        UserAuth get_user_auth = null;
        for (int i = 0; i < len ; i++)
        {
            get_user = all_user.get(i);
            get_user_auth = all_user_auth.get(i);
            UserDTO add_userDTO = new UserDTO(get_user,get_user_auth);
            res.add(add_userDTO);
        }
        return res;
    }

    @Override
    public List<UserAuth> findUserAuthInRange(Integer startId, Integer endId) {
        return userAuthRepository.findUserAuthInRange(startId, endId);
    }


    @Override
    public UserDTO editUser(UserDTO user_dto, String password){
        UserAuth edit_user_auth = new UserAuth(user_dto,password);
        User edit_user = new User(user_dto,edit_user_auth);

        userAuthRepository.save(edit_user_auth);
        userRepository.save(edit_user);

        return user_dto;
    }


    @Override
    public UserAuth createUser(UserDTO user_dto, String password){
        UserAuth add_user_auth =new UserAuth(user_dto,password);    // 制作用户权限
        add_user_auth = userAuthRepository.saveAndFlush(add_user_auth);                     // 分别save
        User add_user = new User(user_dto,add_user_auth);                         // 制作用户信息
        userRepository.save(add_user);                              // save
        return add_user_auth;                                       // 最后返回一个权限表示，如同一个登录
    }

    @Override
    public void deleteUser(Integer userId){
        userAuthRepository.deleteById(userId);
        userRepository.deleteById(userId);
    }
    @Override
    public UserAuth saveAuthAndFlush(UserAuth add){
        return userAuthRepository.saveAndFlush(add);
    }

}
